﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Web;

namespace Gtkj.CMS.App.Models.Weather
{
    public class ccc 
    { 
        public string status{get;set;}
        public string msg { get; set; }
        public result result { get; set; }
    }
    public class result
    {
        /// <summary>
        /// 城市
        /// </summary>
        public string city { get; set; }
       /// <summary>
       /// 城市ID
       /// </summary>
        public int cityid { get; set; }
        /// <summary>
        /// 城市天气代号
        /// </summary>
        public string citycode { get; set; }
        /// <summary>
        /// 日期
        /// </summary>
        public string date { get; set; }

        /// <summary>
        /// 星期
        /// </summary>
        public string week { get; set; }
        /// <summary>
        /// 天气
        /// </summary>
        public string weather { get; set; }
        /// <summary>
        /// 气温
        /// </summary>
        public string temp { get; set; }

        /// <summary>
        /// 最高气温
        /// </summary>
        public string temphigh { get; set; }
        /// <summary>
        /// 最低气温
        /// </summary>
        public string templow { get; set; }
        /// <summary>
        /// 图片数字
        /// </summary>
        public string img { get; set; }
        /// <summary>
        /// 湿度
        /// </summary>
        public string humidity { get; set; }

        /// <summary>
        /// 气压
        /// </summary>
        public string pressure { get; set; }

        /// <summary>
        /// 风俗
        /// </summary>

        public string windspeed { get; set; }

        /// <summary>
        /// 风向
        /// </summary>
        public string winddirect { get; set; }
        /// <summary>
        /// 风级
        /// </summary>
        public string windpower { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public string updatetime { get; set; }

    }
    public class Weather
    {
        private const String host = "http://jisutqybmf.market.alicloudapi.com";
        private const String path = "/weather/query";
        private const String method = "GET";
        private const String appcode = "69d6cd6c14d64b6a9dd3aac6137695c1";

        public ccc WeatherJson(string city)
        {
            String querys = "city=" + city;
            String bodys = "";
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            if (host.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            if (0 < bodys.Length)
            {
                byte[] data = Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }
            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            
            string Json=reader.ReadToEnd();

            ccc ccc = JsonConvert.DeserializeObject<ccc>(Json);
            return ccc;
        }

        private static bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

    }
}